package com.ruoyi.web.controller.minglvmini;


import com.github.pagehelper.Page;
import com.minglv.mini.domain.Goods;
import com.minglv.mini.service.IActivityService;
import com.minglv.mini.service.IGoodsService;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "API-商品")
@RestController
@RequestMapping("/api/goods")
public class ApiGoodsController {

    @Resource
    IGoodsService goodsService;

    @Autowired
    private IActivityService activityService;

    @GetMapping("/list")
    @ApiOperation("商品列表")
    public MiniResult getlist(@RequestParam(name = "page") @ApiParam(value = "页数") Integer page, @RequestParam(name = "type", required = false) @ApiParam(value = "类型") Integer type) {
        Page<Object> objects = PageUtils.startPage(page, 10);
        List<Map> goods = goodsService.getList(type);
        Map data = new HashMap();
        data.put("list",goods);
        data.put("count",objects.getTotal());
        return  MiniResult.success(data);

    }

    @GetMapping("/info")
    @ApiOperation("商品详情")
    public MiniResult getInfo(@RequestParam(name = "id")@ApiParam(value="ID 编号")  Long id){
        Goods goods = goodsService.selectGoodsById(id);

        return MiniResult.success(goods == null ? new ArrayList<>() : goods);

    }

    @GetMapping("/iscollect")
    @ApiOperation("是否收藏")
    public Object iscollect(HttpServletRequest request, @RequestParam(name = "id")@ApiParam(value="ID 编号") Integer id){

        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                Object iscollect = goodsService.iscollect(token, id);
                if (ObjectUtils.isEmpty(iscollect)) {
                    Map reMap = new HashMap();
                    reMap.put("code",401);
                    reMap.put("msg","Token过期,或无此用户");
                    return reMap;
                }else{
                    return MiniResult.success(iscollect);
                }
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

    @PostMapping("/collect")
    @ApiOperation("收藏")
    public Object collect(HttpServletRequest request, @RequestBody() @ApiParam(value = "ID 编号") Map param) {
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                Long id = Long.parseLong(param.get("id").toString());
                Object collect = activityService.collect(token, id);
                if (ObjectUtils.isEmpty(collect)) {
                    Map reMap = new HashMap();
                    reMap.put("code",401);
                    reMap.put("msg","Token过期,或无此用户");
                    return reMap;
                }else{
                    return MiniResult.success(collect);
                }
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }


    @PostMapping("/exchange")
    @ApiOperation("兑换")
    public Object postExchange(HttpServletRequest request, @RequestBody() @ApiParam(value = "ID 编号") Map param) {

        String token = request.getHeader("token");
        if (!StringUtils.isNotEmpty(token)) {
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }

        Long id = Long.parseLong(param.get("id").toString());
        return goodsService.postExchange(token,id);

    }
}
